// 药库管理
<template>
  <el-row class="drugStorage-body">
    <el-col :span="24">
      <TopManu 
      :TopArr="TopArr" 
      @handleServe="()=>{serveData = [],activity = 13,activityInfo = 6,handleGetServiceItem()}" 
      @handleConsumable="()=>{consumableData = [],activity = 12,activityInfo = 6,species_id = 3,getDrugDictIndex()}" @handelDropdown="handelDropdown" 
      @handleDictionaries="()=>{drugStorageData11 = [],activity = 11,activityInfo = 6,species_id = '',getDrugDictIndex()}"  @handelHealth="dialogVisible = true" 
      @handlePut="()=>{activityInfo = 0,activity = 1,getDrugDictIndex()}">
      </TopManu>
    </el-col>
    <el-col :span='24' style="margin-top:20px;padding:0 20px;" class="drugStorage-nav" v-if="activityInfo != 6">
      <div v-if="activityInfo == 0" :class="activity==1?'activity':''" @click="activity = 1">
        入库新增
      </div>
      <div v-if="activityInfo == 0" :class="activity==2?'activity':''" @click="activity = 2">
        入库记录
      </div>
      <div v-if="activityInfo == 1" :class="activity==3?'activity':''" @click="activity = 3">
        出库新增
      </div>
      <div v-if="activityInfo == 1" :class="activity==4?'activity':''" @click="activity = 4">
        出库记录
      </div>
      <div v-if="activityInfo == 2" :class="activity==5?'activity':''" @click="activity = 5">
        领用新增
      </div>
      <div v-if="activityInfo == 2" :class="activity==6?'activity':''" @click="activity = 6">
        领用记录
      </div>
      <div v-if="activityInfo == 3" :class="activity==7?'activity':''" @click="activity = 7">
        回退新增
      </div>
      <div v-if="activityInfo == 3" :class="activity==8?'activity':''" @click="activity = 8">
        回退记录
      </div>
      <div v-if="activityInfo == 4" :class="activity==9?'activity':''" @click="activity = 9">
        采购新增
      </div>
      <div v-if="activityInfo == 4" :class="activity==10?'activity':''" @click="activity = 10">
        采购记录
      </div>
      <div v-if="activityInfo == 5" :class="activity==11?'activity':''" @click="activity = 11">
        盘点新增
      </div>
      <div v-if="activityInfo == 5" :class="activity==12?'activity':''" @click="activity = 12">
        盘点记录
      </div>
    </el-col>
    <el-col :span='24' style="margin-top:20px;padding:0 20px;" class="drugStorage-content" v-if="activity != 11 && activity != 12 && activity != 13">
        <el-form :inline="true" class="demo-form-inline"  label-width="auto" v-model="formInline" style='marginTop:20px;'>
            <el-form-item label="入库单号">
              <el-input placeholder="请输入入库单号" v-model="formInline.rightKeyValue">
              </el-input>
            </el-form-item>
            <el-form-item label="入库库房">
              <el-input el-input placeholder="入库库房" v-model="formInline.rightKeyValue">
                <el-button slot="append" icon="el-icon-search">
                </el-button>
              </el-input>
            </el-form-item>
            <el-form-item label="入库类型">
             <el-select v-model="storageValue" placeholder="请选择">
                <el-option
                  v-for="item in storageType"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="入库凭证">
              <el-input placeholder="请输入姓名/门诊号/身份证号" v-model="formInline.rightKeyValue">
              </el-input>
            </el-form-item>
            <el-form-item label="税务登记号">
              <el-input el-input placeholder="税务登记号" v-model="formInline.rightKeyValue">
              </el-input>
            </el-form-item>
            <el-form-item label="供货单位">
              <el-input placeholder="请输入姓名/门诊号/身份证号" v-model="formInline.rightKeyValue">
              </el-input>
            </el-form-item>
            <el-form-item label="供货员">
              <el-input placeholder="请输入供货员" v-model="formInline.rightKeyValue">
              </el-input>
            </el-form-item>
            <el-form-item label="结算方式">
              <el-select v-model="moneyValue" placeholder="请选择">
                <el-option
                  v-for="item in moneyType"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="折扣金额">
              <el-input el-input placeholder="折扣金额" v-model="formInline.rightKeyValue">
              </el-input>
            </el-form-item>
            <el-form-item label="采购人员">
              <el-input el-input placeholder="采购人员" v-model="formInline.rightKeyValue">
                <el-button slot="append" icon="el-icon-search">
                </el-button>
              </el-input>
            </el-form-item>
            <el-form-item label="验收人">
              <el-input el-input placeholder="验收人" v-model="formInline.rightKeyValue">
                <el-button slot="append" icon="el-icon-search">
                </el-button>
              </el-input>
            </el-form-item>
            <el-form-item label="价格加成">
              <el-input el-input placeholder="价格加成" v-model="formInline.rightKeyValue">
              </el-input>
            </el-form-item>
            <el-form-item label="发票类型">
              <el-select v-model="moneyValue" placeholder="请选择">
                <el-option
                  v-for="item in moneyType"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="发票日期">
              <el-input el-input placeholder="发票日期" v-model="formInline.rightKeyValue">
              </el-input>
            </el-form-item>
        </el-form>
         <FromList :tableTitle="patientTitle" :tableData="patientData" :highlightCurrentRow="highlightCurrentRow" @handleCurrentChange="handleCurrentChange" />
    </el-col>
    <el-col :span='24' style="margin-top:20px;padding:0 20px;" class="drugStorage-content" v-if='activity == 2'>
        <el-col :span="24" style="margin:20px 0 0 0;">
          <ViewTitle text="入库记录" />
        </el-col>
        <el-col :span="24" style="margin:20px 0;">
          <FromList :tableTitle="patientTitle" :tableData="patientData" />
        </el-col>
         <el-col :span="24" style="margin:20px 0 0 0;">
          <ViewTitle text="入库明细" />
        </el-col>
        <el-col :span="24" style="margin:20px 0;">
          <FromList :tableTitle="patientTitle" :tableData="patientData" />
        </el-col>
    </el-col>
    <el-col style="margin-top:20px;padding:0 20px;" :span="24" v-if="activity == 11">
        <ViewTitle text="药品信息" />
        <el-form :inline="true" class="demo-form-inline"  label-width="auto" v-model="drugStorageLine" style='marginTop:20px;'>
            <el-form-item label="药品关键字">
              <el-input placeholder="请输入药品名称" v-model="drugStorageLine.name">
              </el-input>
            </el-form-item>
            <el-form-item label="药品分类">
              <el-input el-input placeholder="请输入药品分类" v-model="drugStorageLine.type">
              </el-input>
            </el-form-item>
            <el-form-item label="费用种类">
              <el-input el-input placeholder="请输入费用种类" v-model="drugStorageLine.kind">
              </el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="getDrugDictIndex()">查询</el-button>
            </el-form-item>
        </el-form>
        <FromList :tableTitle="drugStorage11" :tableData="drugStorageData11" :highlightCurrentRow="drugStorageCurrentRow"
        @handleCurrentChange="drugStorageCurrentChange"
        />
        <ViewTitle text="药品规格"  style="marginTop:20px;" />
        <el-tabs v-model="drugStorageType" style="marginTop:10px;">
          <el-tab-pane label="规格信息" name="1">
            <FromList :tableTitle="specification" :tableData="specificationData"  :highlightCurrentRow="drugStorageCurrentRow"
        @handleCurrentChange="specificationChange" />
          </el-tab-pane>
        </el-tabs>
    </el-col>
    <el-col style="margin-top:20px;padding:0 20px;" :span="24" v-if="activity == 12">
        <ViewTitle text="药品信息" />
        <el-form :inline="true" class="demo-form-inline"  label-width="auto" v-model="drugStorageLine" style='marginTop:20px;'>
            <el-form-item label="药品关键字">
              <el-input placeholder="请输入药品名称" v-model="drugStorageLine.name">
              </el-input>
            </el-form-item>
            <el-form-item label="药品分类">
              <el-input el-input placeholder="请输入药品分类" v-model="drugStorageLine.type">
              </el-input>
            </el-form-item>
            <el-form-item label="费用种类">
              <el-input el-input placeholder="请输入费用种类" v-model="drugStorageLine.kind">
              </el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="getDrugDictIndex()">查询</el-button>
            </el-form-item>
        </el-form>
         <FromList :tableTitle="consumableTitle" :tableData="consumableData" :highlightCurrentRow="drugStorageCurrentRow"
        @handleCurrentChange="drugStorageCurrentChange"
        />
    </el-col>
    <el-col style="margin-top:20px;padding:0 20px;" :span="24" v-if="activity == 13">
        <ViewTitle text="服务信息" />
        <el-form :inline="true" class="demo-form-inline"  label-width="auto" v-model="drugStorageLine" style='marginTop:20px;'>
            <el-form-item label="关键字">
              <el-input placeholder="请输入关键字" v-model="drugStorageLine.name">
              </el-input>
            </el-form-item>
            <el-form-item label="状态">
              <el-radio-group v-model="drugStorageLine.radio">
                <el-radio :label="1">上架</el-radio>
                <el-radio :label="2">下架</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="getDrugDictIndex()">查询</el-button>
            </el-form-item>
             <el-form-item>
            <el-button type="primary" >增加</el-button>
            </el-form-item>
        </el-form>
         <FromList :tableTitle="serveTitle" :tableData="serveData" :highlightCurrentRow="drugStorageCurrentRow"
        @handleCurrentChange="drugStorageCurrentChange"
        />
    </el-col>
    <el-dialog
    title="请输入流水号"
    :visible.sync="numberFlag"
    width="30%"
    >
    <span>
      <el-input placeholder="请输入流水号" v-model="no">
      </el-input>
    </span>
    
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="handleSelect()">确 定</el-button>
    </span>
  </el-dialog>
  </el-row>
</template>

<script>
import FromList from "@/forComponents/fromList/fromList"
import TopManu from "@/components/TopMenu"
import ViewTitle from "@/forComponents/viewTitle"
import {drugStorage11,patientTitle,specification,serveTitle,consumableTitle} from "@/utils/tabsTitle"
import {getDownLoad,getGoodsStore,getGoodsBuy,getGoodsBuyBack,getGoodsSale,getGoodsSaleBack,getGoodsDel,getDrugDictIndex,handleUpCateLog,handleBackCateLog,handleGoodsNumUpdate} from "@/api/drugStorage"
import {handleGetServiceItem} from "@/api/outpatient"
let that 
export default {
  name:"drugStorage",
  data(){
    return {
      consumableTitle:consumableTitle,
      activityInfo:0,
      value: '',
      page:1,
      limit:10,
      index:0,
      activity:1,
      no:"",
      drugStorageCurrentRow:true,
      TopArr:[
        {
          type:1,
          name:"商品盘存"
        },
        
        {
          type:2,
          name:"商品采购"
        },
        {
          type:3,
          name:"商品采购退货"
        },
        {
          type:4,
          name:"商品销售"
        },
        {
          type:5,
          name:"商品销售退货"
        },
        {
          type:6,
          name:"商品信息删除"
        },
        {
          type:7,
          name:"目录对照上传"
        },
        {
          type:8,
          name:"目录对照上传撤销"
        },
        {
          type:9,
          name:"商品库存变更"
        },
      ],
      wardData:[
        {
          wardName:"病区一",
          hold:100,
          null:0
        }
      ],
      drugStorage11:drugStorage11,
      drugStorageLine:{
        kind:"",
        name:"",
        type:""
      },
      specification:specification,
      specificationData:[],
      drugStorageType:"1",
      drugStorageData11:[

      ],
      storageType:[
        {
          value: '1',
          label: '生产入库'
        }, {
          value: '2',
          label: '采购入库'
        },{
          value: '3',
          label: '赠送入库'
        },{
          value: '4',
          label: '借用入库'
        },{
          value: '5',
          label: '退货入库'
        },{
          value: '6',
          label: '保修退回'
        },{
          value: '7',
          label: '临时入库'
        },{
          value: '8',
          label: '其他入库'
        },
      ],
      moneyValue:"",
      moneyType:[
        {
          value: '1',
          label: '现金'
        }, {
          value: '2',
          label: '支付宝'
        },{
          value: '3',
          label: '银行卡'
        },{
          value: '4',
          label: '微信'
        }
      ],
      storageValue:"",
      dialogVisible:false,
      wardTitle:[
        {
            prop:"wardName",
            name:"病区名称",
            type:1,
            width:50
        },
        {
            prop:"hold",
            name:"占",
            type:1,
        },
        {
            prop:"null",
            name:"空",
            type:1,
        }
      ],
      leftValue:"",
      formInline:{
        rightKeyValue:"",
        rightRadio:"0"
      },
      species_id:"",
      drugStorageLine:"",
      patientTitle:patientTitle,
      patientData:
      [
        
      ],
      highlightCurrentRow:true,
      currTab:{

      },
      serveTitle:serveTitle,
      serveData:[
        
      ],
      consumableData:[

      ],
      ServiceItemLimit:500,
      ServiceItemPage:1,
      goods_id:0,
      numberFlag:false,
      numberType:0
    }
  },
  mounted(){
    that = this
    that.getDrugDictIndex()
  },  
  watch:{
    value(to){
      // console.log(to)
      // if(to == 1) that.getEstablishment()
      // if(to == 2) that.getDownLoad()
      // if(to == 3) that.getGoodsStore()
      // if(to == 4) that.getGoodsBuy()
      // if(to == 5) that.getGoodsBuyBack()
      // if(to == 6) that.getGoodsSale()
      // if(to == 7) that.getGoodsSaleBack()
      // if(to == 8) that.getGoodsDel()
    },
    index(to){
      if(to == 0){
        that.loading.close();
      }else{
      that.loading = this.$loading({
          lock: true,
          text: '',
          background: 'rgba(0, 0, 0, 0.7)'
        });
      }
    }
  },
  methods:{
    specificationChange(e){
      that.goods_id = e.id
    },
    handleGoodsNumUpdate(){
      that.index++
      handleGoodsNumUpdate({no:that.no})
      .then(res=>{
        that.index--
        that.numberFlag = false
        this.$message({
          message:"商品库存变更成功",
          type: 'success'
        });
      })
      .catch(e=>{
        that.index--
        that.$message({
          message:e.message,
          type: 'error'
        });
      })
    },
     handleGetServiceItem(){
      that.index++
      handleGetServiceItem({page:that.ServiceItemPage,limit:that.ServiceItemLimit})
      .then(res=>{
        that.index--
        console.log(res)
        res.data.list.forEach((item)=>{
          
        })
        that.serveData = that.serveData.concat(res.data.list)
      })
      .catch(e=>{
        that.index--
        that.$message({
          message:e.message,
          type: 'error'
        });
      })
    },
    getDownLoad(){
      that.index++
      let data = {
        file_qury_no:"fsi/plc/b6c6fdf0fe7d4736897aad05c1e040",
        fixmedins_code:"plc",
        filename:""
      }
      getDownLoad({...data})
          .then(res=>{
            that.index--
            console.log(res)
          })
          .catch(e=>{
            that.index--
            this.$message({
              message:e.message,
              type: 'error'
            });
      })
    },
    getGoodsStore(){
      that.index++
      getGoodsStore({id:that.goods_id,no:that.no})
          .then(res=>{
            that.index--
            that.numberFlag = false
            this.$message({
              message:"盘存成功",
              type: 'success'
            });
          })
          .catch(e=>{
            that.index--
            this.$message({
              message:e.message,
              type: 'error'
            });
      })
    },
    getGoodsBuy(){
      that.index++
      getGoodsBuy({id:that.goods_id,no:that.no})
          .then(res=>{
            that.index--
            that.numberFlag = false
            this.$message({
              message:"采购成功",
              type: 'success'
            });
          })
          .catch(e=>{
            that.index--
            this.$message({
              message:e.message,
              type: 'error'
            });
      })
    },
    getGoodsBuyBack(){
      that.index++
      getGoodsBuyBack({id:that.goods_id,no:that.no})
          .then(res=>{
            that.index--
             that.numberFlag = false
            this.$message({
              message:"采购退货成功",
              type: 'success'
            });
          })
          .catch(e=>{
            that.index--
            this.$message({
              message:e.message,
              type: 'error'
            });
      })
    },
    getGoodsSale(){
      that.index++
      getGoodsSale({id:that.goods_id,no:that.no})
          .then(res=>{
            that.index--
             that.numberFlag = false
            this.$message({
              message:"销售成功",
              type: 'success'
            });
          })
          .catch(e=>{
            that.index--
            this.$message({
              message:e.message,
              type: 'error'
            });
      })
    },
    getGoodsSaleBack(){
      that.index++
      getGoodsSaleBack({id:that.goods_id,no:that.no})
          .then(res=>{
            that.index--
             that.numberFlag = false
            this.$message({
              message:"商品销售退货",
              type: 'success'
            });
          })
          .catch(e=>{
            that.index--
            this.$message({
              message:e.message,
              type: 'error'
            });
      })
    },
    getGoodsDel(){
      that.index++
      getGoodsDel({id:that.goods_id,no:that.no})
      .then(res=>{
        that.index--
        that.numberFlag = false
        this.$message({
          message:"删除成功",
          type: 'success'
        });
      })
      .catch(e=>{
        that.index--
        this.$message({
          message:e.message,
          type: 'error'
        });
    })
    },
    getDrugDictIndex(){
      that.index++
      let data = {
        
      }
      getDrugDictIndex({page:that.page,limit:that.limit,species_id:that.species_id,...data})
      .then(res=>{
        that.index--
        res.data.list.forEach(item=>{
          item.statusFlag = item.status == 1 ? true : false
          item.is_skin_testFlag = item.is_skin_test == 1 ? true : false
          if(item.category !== null){
            item.categoryTitle = item.category.name
          }
          //  if(item.fee_category !== null){
          //   item.fee_categoryTitle = item.fee_category.title
          // }
           if(item.species !== null){
            item.speciesTitle = item.species.name
          }
          //  item.goods_id = item.goods[0].id
           console.log(item)
        })
        
        if(that.activity == 11){
          that.drugStorageData11 = that.drugStorageData11.concat(res.data.list)
        }else if(that.activity == 12){
          that.consumableData = that.consumableData.concat(res.data.list)
        }else if(that.activity == 13){
          that.serveData = that.serveData.concat(res.data.list)
        }
      })
      .catch(e=>{
         that.index--
         console.log(e)
          this.$message({
            message:e.message,
            type: 'error'
          });
      })
    },
    handelDropdown(e){
      if(that.currTab == ""){
        this.$message({
          message:"请选择表单",
          type: 'error'
        });
        return false
      }
      if(e != 7 && e != 8){
        that.numberFlag = true
        that.numberType = e
      }

      if(e == 7){
        that.index++
          handleUpCateLog({goods_id:that.goods_id})
          .then(res=>{
              that.index--
              this.$message({
                message:"目录对照上传成功",
                type: 'success'
              });
          })
          .catch(e=>{
              that.index--
              this.$message({
                message:e.message,
                type: 'error'
              });
          })
      }
      if(e == 8){
        that.index++
        handleBackCateLog({goods_id:that.goods_id})
        .then(res=>{
            that.index--
            this.$message({
              message:"目录对照上传撤销成功",
              type: 'success'
            });
        })
        .catch(e=>{
            that.index--
            this.$message({
              message:e.message,
              type: 'error'
            });
        })
      }
    },
    handleSelect(){
      console.log(that.numberType)
      switch(that.numberType){
        case 1:
          that.getGoodsStore()
        break
        case 2:
          that.getGoodsBuy()
        break
        case 3:
          that.getGoodsBuyBack()
        break
        case 4:
          that.getGoodsSale()
        break
        case 5:
          that.getGoodsSaleBack()
        break
        case 6:
          that.getGoodsSale()
        break
        case 9:
          that.handleGoodsNumUpdate()
        break
      }
    },
    
    handleCurrentChange(e){
      that.currTab = e
      console.log(that.currTab)
    },
    drugStorageCurrentChange(e){
      console.log(e)
      that.specificationData = e.goods
    },
    
  },
  components:{
     FromList,
     TopManu,
     ViewTitle
  }
}
</script>

<style lang="scss" scoped>
  @import "./index.scss";
</style>